package com.chuanyang.driverh.mainhome.fragment;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainhome.FilterActivity;
import com.chuanyang.driverh.mainhome.adapter.GetOrderListAdapter;
import com.chuanyang.driverh.response2.RequestFilter;
import com.chuanyang.driverh.response2.ResultGetOrder;
import com.chuanyang.driverh.response2.ResultGetOrderTop;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.FilterEvent;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class GetOrderListFragment extends BaseListFragment {
    private GetOrderListAdapter listAdapter;
    private View title_bar;
    private RadioGroup rg_top;
    private RadioButton rb0, rb1, rb2, rb3;
    private RequestFilter filter;
    private String showType = "4";//1 累计接单 2当天接单 3当天搁置 4全部订单

    @Override
    public BaseListAdapter createAdapter() {
        return listAdapter = new GetOrderListAdapter(getActivity());
    }

    public void filter() {
        Intent intent = new Intent(activityContext, FilterActivity.class);
        intent.putExtra("type", "1");
        intent.putExtra("data", filter);
        startActivity(intent);
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        title_bar = View.inflate(getContext(), R.layout.home_header_get_order, null);
        rg_top = (RadioGroup) title_bar.findViewById(R.id.rg_top);
        rb0 = (RadioButton) title_bar.findViewById(R.id.rb0);
        rb1 = (RadioButton) title_bar.findViewById(R.id.rb1);
        rb2 = (RadioButton) title_bar.findViewById(R.id.rb2);
        rb3 = (RadioButton) title_bar.findViewById(R.id.rb3);

        if (root.getChildCount() > 0) root.removeAllViews();
        root.addView(title_bar, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    @Override
    protected void initData() {
        filter = new RequestFilter();
        rb0.setText("全部订单(个)" + "\n" +"0" );
        rb3.setText("累计接单(个)" + "\n" + "0");
        rb1.setText("当天接单(个)" + "\n" +"0" );
        rb2.setText("待处理(个)" + "\n" +"0");
        onRefresh();
        getTopData();
    }

    @Override
    protected void initListener() {
        rg_top.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb0:
                        showType = "4";
                        break;
                    case R.id.rb1:
                        showType = "2";
                        break;
                    case R.id.rb2:
                        showType = "3";
                        break;
                    case R.id.rb3:
                        showType = "1";
                        break;

                }
                filter = new RequestFilter();
                onRefresh();
            }
        });

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {


        }
    }

    //先用待处理订单接口
    private void getData() {
        DialogUtil.showLoadingDialog(getActivity(), "加载中...");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("page", mCurrentPage + "");
        params.put("length", "10");
        params.put("showType", showType);
        if (!StringUtil.isBlank(filter.getStartPoint())) {
            params.put("beginCity", filter.getStartPoint());
        }
        if (!StringUtil.isBlank(filter.getEndPoint())) {
            params.put("endCity", filter.getEndPoint());
        }
        if (!StringUtil.isBlank(filter.getStartTime())) {
            params.put("orderTimes", filter.getStartTime());
        }
        if (!StringUtil.isBlank(filter.getEndTime())) {
            params.put("orderTimee", filter.getEndTime());
        }
        if (!StringUtil.isBlank(filter.getSelectType())) {
            params.put("selectType", filter.getSelectType());
        }
      /*  if (filter.getOrderStatus2()!=null){
            params.put("onStatus",filter.getOrderStatus2());
        }
        if (filter.getOrderType2()!=null){
            params.put("orderType",filter.getOrderType2());
        }*/
        if (filter.getOrderStatus2() != null) {//已接单、待处理都选择的时候不传，都不选时候也不传，选择一个的时候才传。
//            params.put("onStatus",StringUtil.getSplitString(filter.getOrderStatus2()));
            if (filter.getOrderStatus2().size() == 2) {
                params.put("showType", "4");
            }
            if (filter.getOrderStatus2().size() == 1) {
                if (filter.getOrderStatus2().get(0) == 1) {
                    params.put("showType", "1");
                } else {
                    params.put("showType", "3");
                }
            }
        }

        if (filter.getOrderType2() != null && filter.getOrderType2().size() > 0) {
//            params.put("orderType",filter.getOrderType2());
            params.put("orderTypes", StringUtil.getSplitString(filter.getOrderType2()));
        }

        MyHttpUtil.getOrderList(getActivity(), params, new MyCallback<ResultGetOrder>(getActivity(), new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(ResultGetOrder result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    try {
                        setDataResult(result.getData().getData());
//                        setHeader(result.getData().getDriverOrderInfoCount());
                    } catch (Exception e) {
                        if (result.getMsg() != null) {
                            PrintUtil.toast(activityContext, result.getMsg());
                        }
                        listAdapter.clear();
                        listAdapter.notifyDataSetChanged();
                    }
                } else {
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(), result.getMsg());
                }
            }
        });
    }

    private void getTopData() {
        DialogUtil.showLoadingDialog(getActivity(), "加载中...");
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("driverId", SharedPrefUtil.getUserId());
        if (!StringUtil.isBlank(SharedPrefUtil.getFleetId())) {
            params.put("fleetId", SharedPrefUtil.getFleetId());
            params.put("type", "2");
        }
        MyHttpUtil.getOrderCountInfo(getActivity(), params, new MyCallback<ResultGetOrderTop>(getActivity(), new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                springView.onFinishFreshAndLoad();
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(ResultGetOrderTop result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    try {
                        setHeader(result.getData());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    springView.onFinishFreshAndLoad();
                    PrintUtil.toast(getActivity(), result.getMsg());
                }
            }
        });
    }


    private void setHeader(ResultGetOrderTop.Data countInfo) {
        if (countInfo == null) return;
    /*    rb0.setText(dispatchInfo.getAllCount()+"\n"+"全部订单");
        rb1.setText(dispatchInfo.getTheCumulativeCount()+"\n"+"累计接单");
        rb2.setText(dispatchInfo.getOnTheDayCount()+"\n"+"当天接单");
        rb3.setText(dispatchInfo.getPutOnHoleCount()+"\n"+"当前待处理");*/

        rb0.setText("全部订单(个)"+ "\n" + countInfo.getAllCount() );
        rb3.setText( "累计接单(个)"+ "\n" + countInfo.getTheCumulativeCount());
        rb1.setText( "当天接单(个)" + "\n" +countInfo.getOnTheDayCount());
        rb2.setText( "待处理(个)" + "\n" +countInfo.getPutOnHoleCount());
    }

    @Override
    public void onGetMore() {
        getData();
    }

    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getData();
    }

    @Override
    protected void onForceRefresh() {
        onRefresh();
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    public void onEventMainThread(FilterEvent event) {
        if ("1".equals(event.getType())) {
            filter = event.getData();
            onRefresh();
        }
    }

    public void onEventMainThread(RefleshFragEvent event) {
        if (event.getIndex() == 0 || event.getIndex() == 1) {
            onRefresh();
        }
    }
}
